//
//  MKUserDefaults.swift
//  Fanmugua
//
//  Created by MK on 2021/3/28.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

let UDStandard = UserDefaults.standard;

class MKUserDefaults: NSObject {
    ///Object
    class func setObject(_ value:Any?, _ key:String) {
        UDStandard.set(value, forKey: key)
        UDStandard.synchronize()
    }
    class func object(_ key:String) -> Any? {
        return UDStandard.object(forKey: key)
    }
    
    ///String
    class func setString(_ value:String?, _ key:String) {
        setObject(value, key)
    }
    class func string(_ key:String) -> String {
        let string = UDStandard.object(forKey: key) as? String
        return string ?? ""
    }
    
    ///Integer
    class func setInteger(_ value:NSInteger, _ key:String) {
        setObject(value, key)
    }
    class func integer(_ key:String) -> NSInteger {
        return UDStandard.integer(forKey: key)
    }
    
    ///Bool
    class func setBool(_ value:Bool, _ key:String) {
        setObject(value, key)
    }
    class func bool(_ key:String) -> Bool {
        return UDStandard.bool(forKey: key)
    }
    
    ///Float
    class func setFloat(_ value:Float, _ key:String) {
        setObject(value, key)
    }
    class func float(_ key:String) -> Float {
        return UDStandard.float(forKey: key)
    }
    
    ///Double
    class func setDouble(_ value:Double, _ key:String) {
        setObject(value, key)
    }
    class func double(_ key:String) -> Double {
        return UDStandard.double(forKey: key)
    }
    
    ///TimeInterval
    class func setTime(_ value:TimeInterval, _ key:String) {
        setInteger(NSInteger(value), key)
    }
    class func time(_ key:String) -> TimeInterval {
        return TimeInterval(integer(key))
    }
    
    ///URL
    class func setURL(_ value:URL?, _ key:String) {
        setObject(value, key)
    }
    class func url(_ key:String) -> URL? {
        return UDStandard.url(forKey: key)
    }
    
    ///删除Key
    class func remove(_ key:String) {
        UDStandard.removeObject(forKey: key)
        UDStandard.synchronize()
    }
    
    ///清除所有
    class func clear() {
        let dict = UDStandard.dictionaryRepresentation()
        for key in dict.keys {
            UDStandard.removeObject(forKey: key)
            UDStandard.synchronize()
        }
    }
}
